package com.ibm.ecm.configmgr.app.acm.widgets.rest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

public class RESTUtilOnATOM extends RESTUtil{
	
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
	}

	
	public HttpResponse executeGet(String url) throws IOException{

			HttpGet getMethod = new HttpGet(url);

			String cookie= "JSESSIONID="+this.client.getParams().getParameter("JSESSIONID").toString()+"; LtpaToken2="+this.client.getParams().getParameter("LtpaToken2").toString();
			getMethod.addHeader("Cookie",cookie);
			
			HttpResponse response=null;
			try {
			response = this.client.execute(getMethod);

			System.out.println("Status code: " + response.getStatusLine().getStatusCode());
			System.out.println("Response: " + response.getStatusLine().getReasonPhrase());

			
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//this.client.getConnectionManager().releaseConnection(this.client.getConnectionManager().getConnection(new HttpRoute(HttpDownload.this.HOST)));
			getMethod.abort();
			return response;
		
	}
	
	public HttpResponse executePost(String url, String content){

			HttpPost postMethod = new HttpPost(url);

			postMethod.addHeader("content-type","application/xml; charset=UTF-8");
			String cred = this.getUsername() + ":" + this.getPassword();
			String authHeader = "Basic "+ new String(Base64.encodeBase64(cred.getBytes()));
			postMethod.addHeader("Authorization", authHeader);
			
			StringEntity requestEntity=null;
			try {
				requestEntity = new StringEntity(content,"UTF-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			postMethod.setEntity(requestEntity);
			
			HttpResponse response = null;
			try{
			response = this.client.execute(postMethod);		 
			}catch (IOException e){
				e.printStackTrace();
			}
			System.out.println("Status code: " + response.getStatusLine().getStatusCode());
			System.out.println("Status Text: " + response.getStatusLine().getReasonPhrase());

		//TODO: convert response to String
		return response;
	}
	
	public HttpResponse executeDelete(String url, String content) {

		HttpDelete deleteMethod = new HttpDelete(url);
		HttpResponse response = null;
		deleteMethod
				.addHeader("content-type", "application/xml; charset=UTF-8");
		String cred = this.getUsername() + ":" + this.getPassword();
		String authHeader = "Basic "
				+ new String(Base64.encodeBase64(cred.getBytes()));
		deleteMethod.addHeader("Authorization", authHeader);

		try {
			response = this.client.execute(deleteMethod);
			System.out.println("Status code: "
					+ response.getStatusLine().getStatusCode());
			System.out.println("Status Text: "
					+ response.getStatusLine().getReasonPhrase());
		} catch (IOException e) {
			e.printStackTrace();
		}
		// TODO: convert response to String
		return response;
	}

}
